layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    /**
     * 监测信息管理
     */
    var TMonitoringinfo = {
        tableId: "tMonitoringinfoTable"
    };

    /**
     * 初始化表格的列
     */
    TMonitoringinfo.initColumn = function () {
        return [[
            {field: 'companyName', sort: true,width:80, title: '所属企业'},
            {field: 'detectTime',width:80, sort: true, title: '监测日期',templet:"<div>{{layui.util.toDateString(d.detectTime, 'yyyy-MM-dd')}}</div>"},
            {field: 'isOrganization', sort: true, title: '有无组织',width:80,templet: function (d) {
                    if(d.isOrganization == 1){
                        return '有组织'
                    } else  {
                        return  '无组织'
                    }
           }},
            {field: 'isInside', sort: true, title: '厂界厂内',width:80,templet: function (d) {
                    if(d.isInside == 1){
                        return '厂界'
                    } else if(d.isInside == 2)  {
                        return  '厂内'
                    } else {
                        return  ''
                    }
                }},
            // {field: 'pollutantEmissions', sort: true, title: '污染物排放量',templet: function (d) {
            //         return '<div onclick=showMedia("img","image","'+d.monitoringReport+'") ><img id="img" src="'+d.monitoringReport+'" alt="" width="100px" height="50px"></ a></div>';
            //     }},
            {field: 'monitoringReport', sort: true, title: '监测报告',templet: '#showScreenhost',   },
            // {field: 'emissionCalculationProcess', sort: true, title: '排放量计算过程',templet: function (d) {
            //         return '<div onclick=showMedia("img","image","'+d.emissionCalculationProcess+'") ><img id="img" src="'+d.emissionCalculationProcess+'" alt="" width="100px" height="50px"></ a></div>';
            //     }},
            {field: 'monitoringPlan', sort: true, title: '自行监测计划',templet: function (d) {
                    return '<div onclick=showMedia("img","image","'+d.monitoringPlan+'") ><img id="img" src="'+d.monitoringPlan+'" alt="" width="100px" height="50px"></ a></div>';
                }},

            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TMonitoringinfo.search = function () {
        var queryData = {};
        queryData['companyName'] = $("#companyName").val();
        table.reload(TMonitoringinfo.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    TMonitoringinfo.openAddDlg = function () {
        func.open({
            title: '添加监测信息',
            content: Feng.ctxPath + '/tMonitoringinfo/add',
            tableId: TMonitoringinfo.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    TMonitoringinfo.openEditDlg = function (data) {
        func.open({
            title: '修改监测信息',
            content: Feng.ctxPath + '/tMonitoringinfo/edit?id=' + data.id,
            tableId: TMonitoringinfo.tableId
        });
    };


    /*点击添加排放口*/
    TMonitoringinfo.openOrificeEdit = function (data) {
        func.open({
            title: '编辑排放口',
            content: Feng.ctxPath + '/tMonitoringinfo/orificeList?id=' + data.id + '&isOrganization=' + 1,
            tableId: TMonitoringinfo.tableId
        });
    };

    /*点击位置编辑*/
    TMonitoringinfo.openPositionEdit = function (data) {
        func.open({
            title: '编辑位置',
            content: Feng.ctxPath + '/tMonitoringinfo/positionEdit?id=' + data.id + '&isOrganization=' + 2 + '&isInside=' + 2,
            tableId: TMonitoringinfo.tableId
        });
    };

    /*上风向*/
    TMonitoringinfo.openUpWind = function (data) {
        func.open({
            title: '编辑上风向',

            content: Feng.ctxPath + '/tPollutantsFactor/positionUpdate?monitoringId=' +data.id + '&isOrganization=' + 2+ '&isInside=' + 1+  '&windType=' + 1,
            tableId: TMonitoringinfo.tableId
        });
    };


    /*下风向1*/
    TMonitoringinfo.openDownWind = function (data) {
        func.open({
            title: '编辑下风向1',
            content: Feng.ctxPath + '/tPollutantsFactor/positionUpdate?monitoringId=' +data.id + '&isOrganization=' + 2+ '&isInside=' + 1+  '&windType=' + 2,
            tableId: TMonitoringinfo.tableId
        });
    };

    /*下风向2*/
    TMonitoringinfo.openDownWind2 = function (data) {
        func.open({
            title: '编辑下风向2',
            content: Feng.ctxPath + '/tPollutantsFactor/positionUpdate?monitoringId=' +data.id + '&isOrganization=' + 2+ '&isInside=' + 1+  '&windType=' + 3,
            tableId: TMonitoringinfo.tableId
        });
    };

    /*下风向3*/
    TMonitoringinfo.openDownWind3 = function (data) {
        func.open({
            title: '编辑下风向3',
            content: Feng.ctxPath + '/tPollutantsFactor/positionUpdate?monitoringId=' +data.id + '&isOrganization=' + 2+ '&isInside=' + 1+  '&windType=' + 4,
            tableId: TMonitoringinfo.tableId
        });
    };


    /**
     * 导出excel按钮
     */
    TMonitoringinfo.exportExcel = function () {
        var checkRows = table.checkStatus(TMonitoringinfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TMonitoringinfo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tMonitoringinfo/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(TMonitoringinfo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TMonitoringinfo.tableId,
        url: Feng.ctxPath + '/tMonitoringinfo/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TMonitoringinfo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TMonitoringinfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TMonitoringinfo.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TMonitoringinfo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TMonitoringinfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TMonitoringinfo.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TMonitoringinfo.onDeleteItem(data);
        }else if (layEvent === 'orificeEdit') {
            TMonitoringinfo.openOrificeEdit(data);
        }else if (layEvent === 'positionEdit') {
            TMonitoringinfo.openPositionEdit(data);
        }else if (layEvent === 'upWind') {
            TMonitoringinfo.openUpWind(data);
        }else if (layEvent === 'downWind') {
            TMonitoringinfo.openDownWind(data);
        }else if (layEvent === 'downWind2') {
            TMonitoringinfo.openDownWind2(data);
        }else if (layEvent === 'downWind3') {
            TMonitoringinfo.openDownWind3(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

         TMonitoringinfo.changeDelFlag(id, checked);
    });

    /**
     * 修改状态
     *
     * @param userId 用户id
     * @param checked 是否选中（true,false），选中就是解锁用户，未选中就是锁定用户
     */
     TMonitoringinfo.changeDelFlag = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/tMonitoringinfo/enable", function (data) {
                if (data.code == 200) {
                    Feng.success("启用成功!");
                }else{
                    Feng.error("启用失败!" + data.message + "!");
                }
                table.reload( TMonitoringinfo.tableId);
            }, function (data) {
                Feng.error("启用失败!" + data.message + "!");
                table.reload( TMonitoringinfo.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/tMonitoringinfo/disable", function (data) {
                if(data.code == 200) {
                    Feng.success("禁用成功!");
                }else{
                    Feng.error("禁用失败!" + data.message + "!");
                }
                table.reload( TMonitoringinfo.tableId);
            }, function (data) {
                Feng.error("禁用失败!" + data.message + "!");
                table.reload( TMonitoringinfo.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        }
    };
});

function showMedia(id, type, src) {
    var $ = layui.jquery;
    var idBar ="#"+id;
    var width = $(idBar).width();
    var height = $(idBar).height();
    var scaleWH = width / height;
    var bigH = 500;
    var bigW = scaleWH * bigH;
    if (bigW > 900) {
        bigW = 900;
        bigH = bigW / scaleWH;
    } // 放大预览图片
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true,
        area: [bigW + 'px', bigH + 'px'], //宽高
        content: "<img width='" + bigW + "' height='" + bigH + "'controls=\"controls\" src=" + src + " />"
    });
}




